package exemples;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Exo2 extends JFrame implements MouseListener,MouseMotionListener{
	
	double Xdeb=0;
	double Ydeb=0;
	Canvas c;
	String type = new String("");
	
	public Exo2(){
		setSize(400,400);
		setTitle("Paint");
		
		JToolBar tb = new JToolBar();
		ButtonGroup bg = new ButtonGroup();
		JRadioButton r1 = new JRadioButton("Small",false);
		JRadioButton r2 = new JRadioButton("Medium",false);
		JRadioButton r3 = new JRadioButton("Large",false);
		r1.addMouseListener(this);
		r2.addMouseListener(this);
		r3.addMouseListener(this);
		
		tb.add(r1);
		tb.add(r2);
		tb.add(r3);
		bg.add(r1);
		bg.add(r2);
		bg.add(r3);
		
		c = new Canvas();
		c.setBackground(Color.WHITE);
		c.addMouseListener(this);
		c.addMouseMotionListener(this);
		
		this.getContentPane().add(tb,BorderLayout.PAGE_START);
		this.getContentPane().add(c);
	}
	
	public void mouseEntered(MouseEvent e){}
	public void mouseExited(MouseEvent e){}
	public void mousePressed(MouseEvent e){
		if(e.getSource()instanceof JRadioButton)
		{
			JRadioButton bouton = (JRadioButton)e.getSource();
			type = bouton.getLabel();
		}
		Xdeb = e.getPoint().getX();
		Ydeb = e.getPoint().getY();
	}
	public void mouseReleased(MouseEvent e){}
	public void mouseClicked(MouseEvent e){}
	
	public void mouseDragged(MouseEvent e){
		if(e.getModifiers()==InputEvent.BUTTON1_MASK)
		{
			double Xfin = e.getPoint().getX();
			double Yfin = e.getPoint().getY();
			Graphics2D g = (Graphics2D)c.getGraphics();
			int taille;
			if(type=="Small"){
				taille = 1;
			}else if(type=="Medium"){
				taille = 3;
			}else{
				taille = 5;
			}
			g.setStroke(new BasicStroke(taille));
			g.drawLine((int)Xdeb,(int)Ydeb,(int)Xfin,(int)Yfin);
			Xdeb = Xfin;
			Ydeb = Yfin;
		}
	}
	public void mouseMoved(MouseEvent e){}
	
	public static void main(String[] args){
		Exo2 test = new Exo2();
		test.setVisible(true);
	}
}